<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use app\admin\model\LogisticsModel;
use app\shop\model\OrderGoodsModel;
use app\shop\model\OrderModel;
use cmf\controller\HomeBaseController;
use think\Db;

class OrderController extends HomeBaseController
{
    function index(){
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $param=$this->request->param();
            if(isset($param['order_no'])){
                $order_weixin=Db::name('order_weixin')->where('order_no_weixin',$param['order_no'])->find();
                if($order_weixin['type']==1){
                       $this->redirect('shop/index/index');
                }
            }           
           $order=new OrderModel();
           $user_orders=$order->get_user_order();
           $this->assign('user_orders',$user_orders);
           return $this->fetch('order');
        }else{
            $this->redirect('shop/index/index');
        }
    }

    function detail(){
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $param=request()->param();
            $id=$param['id'];
            $order=new OrderModel();
            $orders_info=$order->get_order_detail($id);
            $this->assign('orders_info',$orders_info);
            return $this->fetch('orders_info');
        }else{
            $this->redirect('shop/index/index');
        }
    }

    function edit_order(){
        $param=request()->param();
        if($param['type']=='close_order'){
            $order_id=$param['order_id'];
            $rs=Db::name('order')->where('order_id',$order_id)->update(['order_status'=>-1]);
            if($rs){
                $this->success('取消成功');
            }else{
                $this->success('取消失败');
            }
        }
        if($param['type']=='get_order'){
            $order_id=$param['order_id'];
            $data['get_time']=time();
            $data['order_status']=3;
            $rs=Db::name('order')->where('order_id',$order_id)->update($data);
            if($rs){
                $this->success('确认收货成功');
            }else{
                $this->success('确认收货失败');
            }
        }


    }


    function return_order(){
        $param=request()->param();
        if(request()->isPost()){
            $order_id=$param['order_id'];
            $data['refund_require_money']=$param['return_price'];
            $data['refund_require_score']=$param['return_socre'];
            $data['refund_type']=$param['return_type'];
            $data['refund_reason']=$param['text'];
            $data['refund_img']=json_encode($param['img_src']);
            $data['refund_status']=1;
            $data['order_status']=5;
            $data['refund_time']=time();
            Db::name('order')->where('order_id',$order_id)->update($data);
            $this->success('提交成功');
        }else{
            $id=$param['id'];
            $order=new OrderModel();
            $orders_info=$order->get_order_detail($id);
           if($orders_info['order_status']<=3){
               $this->assign('orders_info',$orders_info);
               return $this->fetch();
           }
           if($orders_info['order_status']==4){
               $this->redirect('/user/order/detail/id/'.$id);
           }
            if($orders_info['order_status']>4){
               $logistics=new LogisticsModel();
               $wulius=$logistics->get_list();
                $this->assign('orders_info',$orders_info);

                $newdata=[];
                foreach($wulius as $key=>$value){
                    $newdata[$key]['id']=$value['logistics_code'];
                    $newdata[$key]['value']=$value['logistics_name'];
                    if($orders_info['order_status']>=8&&$orders_info['refund_user_shipping_company']==$value['logistics_code']){
                        $this->assign('o_key',$key);
                        $this->assign('o_name',$value['logistics_name']);
                    }
                }
                $newdata=json_encode($newdata);
                $this->assign('newdata',$newdata);
                return $this->fetch('return_order2');
            }

        }

    }

    function return_order_close(){
        $param=request()->param();
        $order_id=$param['order_id'];
        $data['refund_require_money']=0;
        $data['refund_require_score']=0;
        $data['refund_type']=0;
        $data['refund_reason']='';
        $data['refund_img']='';
        $data['refund_status']=0;
        $data['order_status']=4;
        $data['refund_time']='';
        $data['finish_time']=time();
        Db::name('order')->where('order_id',$order_id)->update($data);
        $this->success('取消成功');
    }


    function return_order_fa(){
        $param=request()->param();
        $order_id=$param['order_id'];
        $data['refund_user_shipping_code']=$param['return_company_code'];
        $data['refund_user_shipping_company']=$param['f_company'];
        $data['order_status']=8;
        $data['refund_userfahuo_time']=time();
        Db::name('order')->where('order_id',$order_id)->update($data);
        $this->success('发货成功');
    }


    function return_order_get(){
        $param=request()->param();
        $order_id=$param['order_id'];
        $data['order_status']=11;
        $data['refund_userget_time']=time();
        Db::name('order')->where('order_id',$order_id)->update($data);
        $this->success('收货成功');
    }
  
  
    function shipping(){
        $param=request()->param();
        $code=$param['code']; 
        $this->assign('code',$code);
        return $this->fetch('shipping');
    }




}
